package com.file.parser;

import com.file.bo.CCB;
import com.file.bo.CCBTran;
import com.file.bo.ResponseData;
import com.file.constant.EfpConstants;
import com.file.constant.ErrorCode;
import com.file.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class CCBExcelParser {
	
	public ResponseData<String> parseCCBExcelToJson(String filePath) {
		log.info("parseCCBExcelToJson started, filePath:{}", filePath);
		String json = null;
		
	    try {
			CCB ccb = parseCCBExcel(filePath);
			json = JsonUtils.convertObjectToJson(ccb);
		} catch (Exception e) {
			log.error("[OnError]parseCCBExcelToJson failed, filePath:{}", filePath, e);
			return new ResponseData<String>(null, ErrorCode.FILE_PARSE_EXCEPTION.getCode(), ErrorCode.FILE_PARSE_EXCEPTION.getMsg());
		}

	    log.info("parseCCBExcelToJson completed, filePath:{}", filePath);
		return new ResponseData<String>(json, ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
	}

	public CCB parseCCBExcel(String filePath) {
		CCB ccb = null;
		try (FileInputStream fis = new FileInputStream(new File(filePath));
				HSSFWorkbook workbook = new HSSFWorkbook(fis)) {
			HSSFSheet sheet = workbook.getSheetAt(0);
			ccb = new CCB();
			List<CCBTran> ccbTrans = new ArrayList<CCBTran>();
			for (int i = 0; i < sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i);
				if (i == 1) {
					ccb.setAccountOpeningInstitution(row.getCell(1).getStringCellValue());
				}
				if (i == 2) {
					ccb.setCurrency(row.getCell(1).getStringCellValue());
				}
				if (i == 3) {
					ccb.setAccountNumber(row.getCell(1).getStringCellValue());
				}

				if (i >= 6) {
					CCBTran ccbTran = new CCBTran();
					ccbTran.setBookkeepingDate(row.getCell(0).getStringCellValue());
					ccbTran.setTransactionDate(row.getCell(1).getStringCellValue());
					ccbTran.setTransactionTime(row.getCell(2).getStringCellValue());
					ccbTran.setExpense(this.getStringCellValue(row.getCell(3)));
					ccbTran.setRevenue(this.getStringCellValue(row.getCell(4)));
					ccbTran.setAccountbalance(this.getStringCellValue(row.getCell(5)));
					ccbTran.setCurrency(row.getCell(6).getStringCellValue());
					ccbTran.setSummary(row.getCell(7).getStringCellValue());
					ccbTran.setCounterPartyAccountNumber(row.getCell(8).getStringCellValue());
					ccbTran.setCounterPartyAccountName(row.getCell(9).getStringCellValue());
					ccbTran.setTradingPlace(row.getCell(10).getStringCellValue());
					ccbTrans.add(ccbTran);
				}
			}
			ccb.setCcbTrans(ccbTrans);
		} catch (Exception e) {
			log.error("[OnError]parseCCBExcel failed", e);
			throw new RuntimeException(e);
		}

		return ccb;
	}
	
	private String getStringCellValue(Cell cell) {
        String cellStr;
        if (Objects.equals(cell.getCellType(), CellType.NUMERIC)) {
                cellStr = String.valueOf(cell.getNumericCellValue());
        } else {
                cellStr = cell.getStringCellValue();
        }
        return cellStr;
}
	
	public static void main(String[] args) throws IOException {
        String pdfFilePath = "E:\\data\\file\\建设银行.xls";
        String jsonFilePath = "E:\\data\\file\\建设银行.json";
        File jsonFile = new File(jsonFilePath);

        CCBExcelParser ccbExcelParser = new CCBExcelParser();
        ResponseData<String> responseData = ccbExcelParser.parseCCBExcelToJson(pdfFilePath);
        log.info(responseData.getData());
        FileUtils.write(jsonFile, responseData.getData(), EfpConstants.UTF_8);
        jsonFile.createNewFile();
	}

}
